package ar.com.uade.administradorsiembra.vista;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import ar.com.uade.administradorsiembra.modelo.exceptions.LoteInexistenteException;
import ar.com.uade.administradorsiembra.views.LoteView;
import ar.com.uade.administradorsiembra.controladores.ABMCerealController;
import ar.com.uade.administradorsiembra.controladores.ABMLoteSimpleController;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class EliminarLoteGUI extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel jCerealLabel;
	private JComboBox jLotesComboBox;
	private JButton jCancelarButton;
	private JButton jEliminarButton;
	private ABMLoteSimpleController loteController;
	private List<LoteView> lotes;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void iniciar() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				EliminarLoteGUI inst = new EliminarLoteGUI();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public EliminarLoteGUI() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			this.loteController = ABMLoteSimpleController.getInstancia();
			this.lotes = this.loteController.getLotes();
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("Eliminar Lote");
			{
				jCerealLabel = new JLabel();
				getContentPane().add(jCerealLabel);
				jCerealLabel.setText("Lote");
				jCerealLabel.setBounds(12, 31, 62, 16);
			}
			{
				ArrayList<String> lotesItem = new ArrayList<String>();
				for (LoteView lote : this.lotes) {
					lotesItem.add(lote.getNumeroLote().toString());
				}
				ComboBoxModel jCerealComboBoxModel = 
					new DefaultComboBoxModel( lotesItem.toArray());
				jLotesComboBox = new JComboBox();
				getContentPane().add(jLotesComboBox);
				jLotesComboBox.setModel(jCerealComboBoxModel);
				jLotesComboBox.setBounds(74, 28, 113, 23);
			}
			{
				jEliminarButton = new JButton();
				getContentPane().add(jEliminarButton);
				jEliminarButton.setText("Eliminar");
				jEliminarButton.setBounds(180, 71, 76, 23);
				jEliminarButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						jEliminarButtonActionPerformed(evt);
					}
				});
			}
			{
				jCancelarButton = new JButton();
				getContentPane().add(jCancelarButton);
				jCancelarButton.setText("Cancelar");
				jCancelarButton.setBounds(272, 71, 79, 23);
				jCancelarButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						System.out.println("jCancelarButton.actionPerformed, event="+evt);
						//TODO add your code for jCancelarButton.actionPerformed
						dispose();
					}
				});
			}
			pack();
			this.setSize(400, 155);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void jEliminarButtonActionPerformed(ActionEvent evt) {
		System.out.println("jEliminarButton.actionPerformed, event="+evt);
		try {
			this.loteController.borrarLoteSimple(
					this.lotes.get(this.jLotesComboBox.getSelectedIndex()).getNumeroLote()
					);
		} catch (LoteInexistenteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
